import { post, get, patch } from '@/api/servicerequest';
import { EiInfo } from '@eplat/ei';
import { useUserStore } from '@/stores/user';
import { Toast } from 'vant';
const { VITE_MOBILE_SERVICE } = import.meta.env;

const SERVICE = {

  //网页授权access_token openid
  wx_sq(obj) {
    const url ='https://api.weixin.qq.com/sns/oauth2/access_token?appid='+api4j.APPID+'&secret=9748410a55297fafd641720151c3a17e&code=:code&grant_type=authorization_code'
    const finalUrl = url.replace(':code', obj);
    return callget(finalUrl, obj);
  },




  /*
   join: client
   s: {"status": "待受理"}
   sort: createdAt,ASC
   /* 送检申请处理列表 api_apply_sqcl */
  api_apply_sqcl(obj) {
    return callget(VITE_MOBILE_SERVICE + 'apply?join=client&s={%22status%22:%20%22%E5%BE%85%E5%8F%97%E7%90%86%22}&sort=createdAt,ASC', obj);
  },
  api_apply_sl(id) {
    return callpatch(VITE_MOBILE_SERVICE + 'apply/handle', {action:'accept',payload:[id]});
  },
  api_apply_jj(id,msg) {
    return callpatch(VITE_MOBILE_SERVICE + 'apply/handle', {action:'denial',payload:[id],reply:msg});
  },
  api_apply_add(obj) {
    return call(VITE_MOBILE_SERVICE + 'apply', obj);
  },
  // 送检申请 apply_byUs
  apply_byUs(obj) {
    return call(VITE_MOBILE_SERVICE + 'apply/byUs', obj);
  },
  // 处理申请 受理或者拒绝apply_handle
  apply_handle(obj) {
    return call(VITE_MOBILE_SERVICE + 'apply/handle', obj);
  },
  //  送检接受get
  api_apply_sjjs(obj) {
    return callget(
      VITE_MOBILE_SERVICE +
        'apply?join=client&s={%22status%22:%20%22%E5%B7%B2%E5%8F%97%E7%90%86%22}&sort=createdAt,ASC',
      obj
    );
  },

  // 接受送检 client
  api_client(obj) {
    return callget(VITE_MOBILE_SERVICE + 'client', obj);
  },
    // 根据id接受
  api_apply_id(obj){
      const url = `apply/:id?join=client`;
      const finalUrl = url.replace(':id', obj);
      return callget(VITE_MOBILE_SERVICE + finalUrl);
  },

  // 接受送检 task
  api_apply_task(obj) {
    return call(VITE_MOBILE_SERVICE + 'task', obj);
  },
  // 接受送检 taskno
  api_apply_taskno(obj) {
    return call(VITE_MOBILE_SERVICE + 'task', obj);
  },

  // 获取器具
  api_apply_lqqj(obj) {
    return callget(VITE_MOBILE_SERVICE + 'task/action?stage=%E9%A2%86%E5%8F%96', obj);
  },

  // 领取器具
  api_apply_lqqjs(obj) {
    const url = `task/:id`;
    const finalUrl = url.replace(':id', obj.id);
    return callpatch(VITE_MOBILE_SERVICE + finalUrl,obj);
  }

};

export function call(url, ei) {
  let  params ={};
  if(typeof(ei)=='object'){
    params=ei;
  }else if(ei==null){}else{
    params = JSON.parse(ei);
  }

  return new Promise((resolve, reject) => {
    post(url,params)
      .then(async (res) => {
        if(typeof(res)=='object'){
          resolve(res);return;
        }
        const obj = JSON.parse(res);
        resolve(obj);return;
        const data = EiInfo.parseJSONObject(res);
        if(data==null){
          resolve(data);return;
        }
        if(data.hasOwnProperty("status")==false){
          resolve(data);return;
        }
        if (data.getStatus() === -1 && data.getMsg().indexOf('票据过期') > -1) {
          Toast(data.getMsg());
          const user = useUserStore();
          await user.logout();
        } else {
          resolve(data);
        }
      })
      .catch((err) => {
        reject(err);
      });
  });
}

export function callget(url, ei) {
  let  params ={};
  if(typeof(ei)=='object'){
    params=ei;
  }else if(ei==null){}else{
    params = JSON.parse(ei);
  }

  return new Promise((resolve, reject) => {
    get(url, params)
      .then(async (res) => {
        if(typeof(res)=='object'){
          resolve(res);return;
        }
        const obj = JSON.parse(res);
        resolve(obj);return;
        const data = EiInfo.parseJSONObject(res);
        if(data==null){
          resolve(data);return;
        }
        if(data.hasOwnProperty("status")==false){
          resolve(data);return;
        }
        if (data.getStatus() === -1 && data.getMsg().indexOf('票据过期') > -1) {
          Toast(data.getMsg());
          const user = useUserStore();
          await user.logout();
        } else {
          resolve(data);
        }
      })
      .catch((err) => {
        reject(err);
      });
  });
}
export function callpatch(url, ei) {
  let  params ={};
  if(typeof(ei)=='object'){
    params=ei;
  }else if(ei==null){}else{
    params = JSON.parse(ei);
  }

  return new Promise((resolve, reject) => {
    patch(url, params)
      .then(async (res) => {
        if(typeof(res)=='object'){
          resolve(res);return;
        }
        const obj = JSON.parse(res);
        resolve(obj);return;
        const data = EiInfo.parseJSONObject(res);
        if(data==null){
          resolve(data);return;
        }
        if(data.hasOwnProperty("status")==false){
          resolve(data);return;
        }
        if (data.getStatus() === -1 && data.getMsg().indexOf('票据过期') > -1) {
          Toast(data.getMsg());
          const user = useUserStore();
          await user.logout();
        } else {
          resolve(data);
        }
      })
      .catch((err) => {
        reject(err);
      });
  });
}
export default SERVICE;
